package app.goods.info.utils;

import java.text.DecimalFormat;

public class GeoUtil {
	// 地球半径（单位：米）
	private static final double EARTH_RADIUS = 6371000;

	// 将角度转换为弧度
	private static double toRadians(double angle) {
		return Math.toRadians(angle);
	}

	// 将弧度转换为角度
	private static double toDegrees(double radians) {
		return Math.toDegrees(radians);
	}

	// 计算新的经纬度
	public static double[] calculateNewCoordinate(double lat, double lon, double distance, double bearing) {
		double angularDistance = distance / EARTH_RADIUS;

		double lat1 = toRadians(lat);
		double lon1 = toRadians(lon);

		double lat2 = Math.asin(Math.sin(lat1) * Math.cos(angularDistance)
				+ Math.cos(lat1) * Math.sin(angularDistance) * Math.cos(toRadians(bearing)));
		double lon2 = lon1 + Math.atan2(Math.sin(toRadians(bearing)) * Math.sin(angularDistance) * Math.cos(lat1),
				Math.cos(angularDistance) - Math.sin(lat1) * Math.sin(lat2));

		lon2 = (lon2 + 3 * Math.PI) % (2 * Math.PI) - Math.PI; // 将结果转换为-180到180之间

		return new double[] { toDegrees(lat2), toDegrees(lon2) };
	}

	public static void main(String[] args) {
		double centerLat = 34.22;
		double centerLon = 113.34;
		double radius = 500; // 半径为500米

		// 八个方向的方位角（北、东北、东、东南、南、西南、西、西北）
		double[] bearings = { 0, 45, 90, 135, 180, 225, 270, 315 };

		DecimalFormat df = new DecimalFormat("#.######");

		for (double bearing : bearings) {
			double[] newCoordinate = calculateNewCoordinate(centerLat, centerLon, radius, bearing);
			System.out.println("Bearing: " + df.format(toDegrees(bearing)) + "° -> Lat: " + df.format(newCoordinate[0])
					+ ", Lon: " + df.format(newCoordinate[1]));
		}
	}
}